'''
    复制/黏贴 有bug待修复

    鼠标右键菜单
        跳出菜单
            menu.post(x,y)
        绑定菜单操作
            menu.add_command(label='菜单'，command=func)

'''

import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
from tkinter import simpledialog as sd
from tkinter import colorchooser as ch


class application(tk.Frame):
    def __init__(self,master=None):
        self.master=master
        super().__init__(self.master)
        self.fileName=None
        self.path = '/Users/zhanglu/Desktop/学习/编程/python/tkinter/tkinter/tkinter/'
        self.creatWidget()

    def creatWidget(self):
        # 主菜单
        self.menubar=tk.Menu(self.master)
        self.menuFile=tk.Menu(self.menubar)
        self.menuEdit=tk.Menu(self.menubar)
        self.menuHelp=tk.Menu(self.menubar)

        self.menubar.add_cascade(label='文件',menu=self.menuFile)
        self.menubar.add_cascade(label='编辑',menu=self.menuEdit)
        self.menubar.add_cascade(label='帮助',menu=self.menuHelp)

        self.menuFile.add_command(label='新建',accelerator="ctrl+n",command=self.creat_)
        self.master.bind('<Control-n>',lambda event:self.creat_())
        self.menuFile.add_command(label='打开',accelerator='ctrl+o',command=self.open_)
        self.master.bind("<Control-o>",lambda event:self.open_())
        self.menuFile.add_command(label='保存',accelerator='ctrl+s',command=self.save_)
        self.menuFile.bind('<Control-s>',lambda event:self.save_())
        self.menuFile.add_command(label='另存为',accelerator='ctrl+a',command=self.asSave_)
        self.menuFile.bind('<Control-a>',lambda event:self.asSave_())
        self.menuFile.add_separator()
        self.menuFile.add_command(label='退出',accelerator='ctrl+q',command=self.quit_)
        self.menuFile.bind('<Control-q>',lambda event:self.quit_())

        self.menuEdit.add_command(label='复制',accelerator='ctrl+c',command=self.copy_)
        self.menuEdit.add_command(label='黏贴',accelerator='ctrl+v',command=self.paste_)
        self.menuEdit.add_command(labe='字体大小',accelerator='ctrl+f',command=self.fontSize_)
        self.menuEdit.add_command(labe='字体颜色', accelerator='ctrl+f', command=self.fontColor_)
        self.master['menu']=self.menubar
        # 右键菜单
        self.mouseMenu=tk.Menu(self.master)
        self.mouseMenu.add_command(label='背景色',command=self.backGround)
        self.master.bind("<Control-Button-1>",self.menuPost)

        self.t1=tk.Text()
        self.t1.pack()

    def menuPost(self,event):
        self.mouseMenu.post(event.x_root, event.y_root)

    def backGround(self):
        color=ch.askcolor()[-1]
        self.t1.config(bg=color)

    def test(self):
        messagebox.showinfo('测试','试试有没有反应')

    def fontColor_(self):
        color=ch.askcolor(title='选择文字颜色')[-1]
        self.t1.config(fg=color)

    def fontSize_(self):
        size=sd.askinteger(title='设置字号',prompt='请输入字号',initialvalue=14,minvalue=8,maxvalue=100)
        newFont=('arial',size)
        self.t1.config(font=newFont)

    def copy_(self):
        self.t1.tag_add('sel',tk.CURRENT)
        self.t1.tag_config('sel',foreground='red')
        self.txt=self.t1.selection_get()
        messagebox.showinfo('test',self.txt)

    def paste_(self):
        self.t1.insert(tk.INSERT,self.txt)

    def quit_(self):
        result=messagebox.askokcancel('请选择','是否保存文件？')
        if result==True:
            if self.fileName:
                self.save_()
            else:
                self.asSave_()
        self.master.destroy()

    def open_(self):
         self.t1.delete(1.0,tk.END)
         fileName=filedialog.askopenfilename(initialdir=self.path)
         self.fileName = fileName
         if self.fileName:
             with open(self.fileName,mode='r',encoding='utf-8') as f:
                self.t1.insert(tk.END,f.read())
                self.winfo_toplevel().title(self.fileName)

    def creat_(self):
        # fileName=sd.askstring(title='打开文件',prompt='请输入文件名')
        # self.fileName=self.path+fileName
        # self.f=open(self.fileName,mode='w',encoding='utf-8')
        # self.f.close()
        self.t1.delete(1.0, tk.END)

    def asSave_(self):
         # fileName=sd.askstring(title='文件另存为',prompt='请输入需要保存的文件名')
         # self.fileName=self.path+fileName
         # with open(self.fileName,mode='w',encoding='utf-8') as f:
         #    txt=self.t1.get(1.0,tk.END)
         #    if txt:
         #        f.write(txt)
         #    messagebox.showinfo('保存文件','文件已保存')
        self.fileName=filedialog.asksaveasfilename(title='文件另存为')
        if self.fileName:
            with open(self.fileName,mode='w',encoding='utf-8') as f:
                content=self.t1.get(1.0,tk.END)
                f.write(content)
            fileName=self.fileName.split('/')[-1]
            messagebox.showinfo('保存文件',f'{fileName}已保存')
            self.winfo_toplevel().title(fileName)

    def save_(self):
        if self.fileName:
            with open(self.fileName,mode='w',encoding='utf-8') as f:
                content=self.t1.get(1.0,tk.END)
                f.write(content)
            messagebox.showinfo('保存文件','文件已保存')
            fileName=self.fileName.split('/')[-1]
            self.winfo_toplevel().title(fileName)
        else:
            self.asSave_()

title='文本编辑器'
window=tk.Tk()
window.title(title)
app=application(window)
window.mainloop()